/**
 * Settle模块 - 提现管理API
 * @description 提现管理相关的API接口定义
 * @author AI Assistant
 * @since 1.0.0
 */

import request from '@/config/axios'
import type {
  WithdrawApplyVO,
  WithdrawApplyCreateReqVO,
  WithdrawApplyPageReqVO,
  WithdrawAuditReqVO,
  PageResult
} from './types'

/**
 * 获取提现申请分页列表
 * @param params 分页查询参数
 * @returns 提现申请分页数据
 */
export const getWithdrawApplyPageApi = (
  params: WithdrawApplyPageReqVO
): Promise<PageResult<WithdrawApplyVO>> => {
  return request.get({ url: '/settle/withdraw-apply/page', params })
}

/**
 * 获取提现申请详情
 * @param id 申请ID
 * @returns 提现申请详情
 */
export const getWithdrawApplyApi = (id: number): Promise<WithdrawApplyVO> => {
  return request.get({ url: `/settle/withdraw-apply/get?id=${id}` })
}

/**
 * 创建提现申请
 * @param data 提现申请创建数据
 * @returns 创建结果
 */
export const createWithdrawApplyApi = (data: WithdrawApplyCreateReqVO): Promise<number> => {
  return request.post({ url: '/settle/withdraw-apply/create', data })
}

/**
 * 取消提现申请
 * @param id 申请ID
 * @param remark 取消原因
 * @returns 取消结果
 */
export const cancelWithdrawApplyApi = (id: number, remark?: string): Promise<boolean> => {
  return request.put({
    url: '/settle/withdraw-apply/cancel',
    data: { id, remark }
  })
}

/**
 * 审核提现申请
 * @param data 审核数据
 * @returns 审核结果
 */
export const auditWithdrawApplyApi = (data: WithdrawAuditReqVO): Promise<boolean> => {
  return request.put({ url: '/settle/withdraw-apply/audit', data })
}

/**
 * 批量审核提现申请
 * @param ids 申请ID列表
 * @param auditStatus 审核状态: 1-通过 2-拒绝
 * @param auditRemark 审核备注
 * @returns 批量审核结果
 */
export const batchAuditWithdrawApplyApi = (
  ids: number[],
  auditStatus: number,
  auditRemark?: string
): Promise<boolean> => {
  return request.put({
    url: '/settle/withdraw-apply/batch-audit',
    data: { ids, auditStatus, auditRemark }
  })
}

/**
 * 执行提现
 * @param id 申请ID
 * @returns 执行结果
 */
export const executeWithdrawApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/withdraw-apply/execute?id=${id}` })
}

/**
 * 批量执行提现
 * @param ids 申请ID列表
 * @returns 批量执行结果
 */
export const batchExecuteWithdrawApi = (ids: number[]): Promise<boolean> => {
  return request.post({
    url: '/settle/withdraw-apply/batch-execute',
    data: { ids }
  })
}

/**
 * 手动同步提现状态
 * @param id 申请ID
 * @returns 同步结果
 */
export const syncWithdrawStatusApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/withdraw-apply/sync-status?id=${id}` })
}

/**
 * 重新提现
 * @param id 申请ID
 * @returns 重新提现结果
 */
export const retryWithdrawApi = (id: number): Promise<boolean> => {
  return request.post({ url: `/settle/withdraw-apply/retry?id=${id}` })
}

/**
 * 获取提现统计数据
 * @param params 查询参数
 * @returns 统计数据
 */
export const getWithdrawStatisticsApi = (params: {
  merchantNo?: string
  dateRange?: [string, string]
}): Promise<{
  totalAmount: number // 总提现金额
  totalCount: number // 总提现笔数
  pendingAmount: number // 待审核金额
  pendingCount: number // 待审核笔数
  approvedAmount: number // 已审核金额
  approvedCount: number // 已审核笔数
  successAmount: number // 提现成功金额
  successCount: number // 提现成功笔数
  failedAmount: number // 提现失败金额
  failedCount: number // 提现失败笔数
  totalFee: number // 总手续费
}> => {
  return request.get({ url: '/settle/withdraw-apply/statistics', params })
}

/**
 * 获取商户提现配置
 * @param merchantNo 商户编号
 * @returns 提现配置
 */
export const getWithdrawConfigApi = (
  merchantNo: string
): Promise<{
  minWithdrawAmount: number // 最小提现金额
  maxWithdrawAmount: number // 最大提现金额
  dailyWithdrawLimit: number // 每日提现限额
  withdrawFeeRate: number // 提现手续费率(%)
  withdrawFixedFee: number // 提现固定手续费
  autoAudit: boolean // 是否自动审核
  autoAuditAmount: number // 自动审核金额阈值
}> => {
  return request.get({ url: `/settle/withdraw-apply/config?merchantNo=${merchantNo}` })
}

/**
 * 导出提现申请数据
 * @param params 查询参数
 * @returns 导出文件
 */
export const exportWithdrawApplyApi = (params: WithdrawApplyPageReqVO): Promise<void> => {
  return request.download({
    url: '/settle/withdraw-apply/export',
    params
  })
}
